package cinema.Controller;

import cinema.Dao.MovieDAO;
import cinema.Dao.RoomDAO;
import cinema.Dao.TimeTableDAO;
import cinema.Entity.Movie;
import cinema.Entity.Room;
import cinema.Entity.TimeTable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("serial")
@Named
@ViewScoped
public class ShowingController implements Serializable {

    @Inject
    private RoomDAO roomDAO;
    @Inject
    private TimeTableDAO timeTableDAO;
    @Inject
    private MovieDAO movieDAO;

    private Date startTime;
    private int roomId;
    private int movieId;

    private Date editStartTime;
    private int editRoomId;
    private int editMovieId;

    private TimeTable timeTable;

    private List<Boolean> canEdit;

    private List<TimeTable> timeTables;
    private List<Room> rooms;
    private List<Movie> movies;

    @PostConstruct
    public void init() {
        timeTables = timeTableDAO.findAll();
        rooms = roomDAO.findAll();
        movies = movieDAO.findAll();
        canEdit = new ArrayList<Boolean>();
        for (int i = 0; i < timeTables.size(); ++i) canEdit.add(false);
    }

    public void addTimeTable() {
        if (startTime != null) {
            TimeTable timeTable = new TimeTable(movieDAO.findById(movieId), roomDAO.findById(roomId), startTime);
            for(TimeTable t : timeTables){
                if(t.getRoom().equals(timeTable.getRoom())){
                    Calendar compareTime = Calendar.getInstance();
                    compareTime.setTime(t.getStartTime());
                    compareTime.add(Calendar.MINUTE,t.getMovie().getLength() + 15);
                    if(compareTime.getTime().after(timeTable.getStartTime())){
                        compareTime.setTime(t.getStartTime());
                        compareTime.add(Calendar.MINUTE, -1 * (timeTable.getMovie().getLength() + 15));
                        if(compareTime.getTime().before(timeTable.getStartTime())){
                            FacesContext.getCurrentInstance().addMessage("inputDate",
                                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "There is already a showing registered in this room in this time", "There is already a showing registered in this room in this time"));
                            return;
                        }
                    }
                }
            }
            timeTables.add(timeTable);
            canEdit.add(false);
            timeTableDAO.save(timeTable);
        }
    }

    public void deleteTimeTable() {
        timeTableDAO.delete(timeTable);
        canEdit.remove(timeTables.indexOf(timeTable));
        timeTables.remove(timeTable);
    }

    public void editTimeTable() {
        canEdit.set(timeTables.indexOf(timeTable), true);
    }

    public void saveTimeTable() {
        canEdit.set(timeTables.indexOf(timeTable), false);
        timeTableDAO.update(timeTable);
    }

    public int getRoomId() {
        return roomId;
    }

    public void setRoomId(int roomId) {
        this.roomId = roomId;
    }

    public int getEditRoomId() {
        return editRoomId;
    }

    public void setEditRoomId(int editRoomId) {
        this.editRoomId = editRoomId;
        this.timeTable.setRoom(roomDAO.findById(editRoomId));
    }

    public int getEditMovieId() {
        return editMovieId;
    }

    public void setEditMovieId(int editMovieId) {
        this.editMovieId = editMovieId;
        this.timeTable.setMovie(movieDAO.findById(editMovieId));
    }

    public Date getEditStartTime() {
        return editStartTime;
    }

    public void setEditStartTime(Date editStartTime) {
        this.editStartTime = editStartTime;
        this.timeTable.setStartTime(editStartTime);
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public int getMovieId() {
        return movieId;
    }

    public void setMovieId(int movieId) {
        this.movieId = movieId;
    }

    public TimeTable getTimeTable() {
        return timeTable;
    }

    public void setTimeTable(TimeTable timeTable) {
        this.timeTable = timeTable;
        this.editMovieId = timeTable.getMovie().getmId();
        this.editRoomId = timeTable.getRoom().getRoomId();
        this.editStartTime = timeTable.getStartTime();
    }

    public List<TimeTable> getTimeTables() {
        return timeTables;
    }

    public void setTimeTables(List<TimeTable> timeTables) {
        this.timeTables = timeTables;
    }

    public List<Room> getRooms() {
        return rooms;
    }

    public void setRooms(List<Room> rooms) {
        this.rooms = rooms;
    }

    public List<Movie> getMovies() {
        return movies;
    }

    public void setMovies(List<Movie> movies) {
        this.movies = movies;
    }

    public List<Boolean> getCanEdit() {
        return canEdit;
    }

    public void setCanEdit(List<Boolean> canEdit) {
        this.canEdit = canEdit;
    }
}
